package com.lizhi.menu.common.utils;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author lizhi
 * @description MybatisPlusHandler自动填充拦截器
 */
@Component
public class MybatisPlusHandler implements MetaObjectHandler {

    /**
     * 新增填充
     *
     * @description: insertFill
     * @author: lizhi
     * @return: void
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime date = LocalDateTime.now();
        LocalDate now = LocalDate.now();
        this.setFieldValByName("id", SnowFlake.nextId(), metaObject);
        this.setFieldValByName("createTime", date, metaObject);
        this.setFieldValByName("updateTime", date, metaObject);
    }

    /**
     * 编辑填充时间
     *
     * @description: updateFill
     * @author: lizhi
     * @return: void
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        LocalDateTime date = LocalDateTime.now();
        //自动填充时间
        this.setFieldValByName("updateTime", date, metaObject);
    }

}
